package com.kantboot.api.email.repository;

import com.kantboot.api.email.domain.dto.ApiEmailSearchDTO;
import com.kantboot.api.email.domain.entity.ApiEmail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ApiEmailRepository extends JpaRepository<ApiEmail,Long> {


    @Query("""
    FROM ApiEmail AS e
    WHERE
    (:#{#param.typeCode} IS NULL OR e.typeCode = '' OR e.typeCode = :#{#param.typeCode})
    AND (:#{#param.to} IS NULL OR e.to = :#{#param.to})
    AND (:#{#param.subject} IS NULL OR e.subject LIKE CONCAT('%',:#{#param.subject},'%'))
    AND (:#{#param.content} IS NULL OR e.content LIKE CONCAT('%',:#{#param.content},'%'))
    AND (:#{#param.isHtml} IS NULL OR e.isHtml = :#{#param.isHtml})
    AND (:#{#param.statusCode} IS NULL OR e.statusCode = '' OR e.statusCode = :#{#param.statusCode})
    ORDER BY e.id DESC
""")
    Page<ApiEmail> getBodyData(
            @Param("param") ApiEmailSearchDTO param, Pageable pageable);
}
